package org.bikelab.se3.query;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.FSDirectory;
import org.sindice.siren.search.SirenCellClause;
import org.sindice.siren.search.SirenCellQuery;
import org.sindice.siren.search.SirenTermQuery;

public class Se3Searcher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			String path = args[0] + "/idx";
			FSDirectory dir = FSDirectory.open(new File(path));
			IndexReader reader = IndexReader.open(dir, true);
			
			IndexSearcher searcher = new IndexSearcher(dir);
			ScoreDoc[] results = searcher.search(getQuery1(args[1]), null, 10).scoreDocs;
		    System.out.println("Number of hits: " + results.length);
		    for (final ScoreDoc doc : results) {
		      System.out.println(doc.doc + ": " + reader.document(doc.doc));
		      System.out.println(doc.score);
		    }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	  public static Query getQuery1(String k) {
		    // Create a cell query matching either the keyword "renaud" or the full URI
		    // "http://renaud.delbru.fr/rdf/foaf#me".
		  System.out.println("Keyword : " + k);
		    final SirenCellQuery cq = new SirenCellQuery();
		    cq.add(new SirenTermQuery(new Term("content", k)), SirenCellClause.Occur.SHOULD);
//		    cq.add(new SirenTermQuery(new Term("content", "http://example.org/alice/foaf.rdf#me")), SirenCellClause.Occur.SHOULD);
		    // Constraint the cell index to 0 (first column: subject position)
//		    cq.setConstraint(2);

		    return cq;
		  }

}
